package com.example.demo.mapper;

import com.example.demo.entity.SearchHistory;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface SearchHistoryMapper {
    // 查找特定用户的搜索的关键词
    @Select("SELECT * from search_history where user_id = #{userId} order by search_time desc")
    List<SearchHistory> searchHistoryByUserIdBySearchTimeDesc(Long userId);

    // 插入一条历史
    @Insert("INSERT INTO search_history(user_id, keyword, search_time) VALUES(#{userId}, #{keyword}, NOW())")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int insert(SearchHistory history);

    // 根据ID查找历史
    @Select("SELECT * FROM search_history WHERE id = #{id}")
    SearchHistory selectById(Long id);

    // 删除指定用户的所有搜索历史
    @Delete("DELETE FROM search_history WHERE user_id = #{userId}")
    int deleteByUserId(@Param("userId") Long userId);
}
